

import bpy

import 全局控制.Node.节点和插座基类.bl_节点基类 as 基类
import 全局控制.Node.插座.bl_插座基类 as 插座基类

import engine.initLIB.load_dll as engineLib
import ctypes
import math


from 物体.物体管理 import engine_物体创建





def f_update_平面方向(self, context):
	location = (0.0,0.0,0.0)
	rot = (0.0,0.0,0.0)
	物体ID = 0;
	if self.m_物体 != None :
		location = tuple(self.m_物体.location)
		rot = (math.degrees(self.m_物体.rotation_euler.x), math.degrees(self.m_物体.rotation_euler.y), math.degrees(self.m_物体.rotation_euler.z))
		
		engine_物体创建.f_添加虚拟物体到引擎(self.m_物体)
		物体ID = int(self.m_物体.m_物体ID)
		
	#data = (location, rot, int(self.m_转坐标系), 物体ID, list(self.m_物体.name))
	data = (location, rot, int(self.m_转坐标系), 物体ID, self.m_物体)
	engineLib.flexCAPI.f_node_传入BL数据(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), ctypes.py_object(data));
	
class C_Node_Px平面(基类.C_Node_基类):
	bl_dname = 'bl_node_Px平面'
	bl_label = '创建 PhysX 平面'

	m_物体 = bpy.props.PointerProperty(type=bpy.types.Object, update=f_update_平面方向)
	m_转坐标系 = bpy.props.BoolProperty(default=True)
	def init(self, context):
		super(C_Node_Px平面, self).f_init(context)
		self.outputs.new('C_Socket_物理体', name='物理体')
		self.f_toCPP()
		
	def f_toCPP(self):
		engineLib.flexCAPI.f_创建物理平面节点(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), self.f_getPyObject());
		f_update_平面方向(self, None)

	def draw_buttons(self, context, layout):
		layout.prop(self, "m_物体")
		layout.prop(self, "m_转坐标系")
		
	def f_update(self):
		f_update_平面方向(self, None)


class C_NODE_AddMenu_Px平面(bpy.types.Operator):
	bl_idname = 'node.global_node_create_px_plane'
	bl_label = '创建 PhysX 平面'
	bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		bpy.ops.node.add_node(type='C_Node_Px平面', use_transform=True)
		return {'FINISHED'}



'''
class C_Node_Px平面(基类.C_Node_基类):
	bl_dname = 'bl_node_Px平面'
	bl_label = '创建 PhysX 平面'

	def init(self, context):
		super(C_Node_Px平面, self).f_init(context)
		self.inputs.new('C_Socket_F32', name='距离')
		self.inputs.new('C_Socket_Vec3', name='方向')
		self.outputs.new('C_Socket_Vec3', name='Px平面')
		self.f_toCPP()

	def f_toCPP(self):
		engineLib.flexCAPI.f_创建车轮节点(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name));
		
		#设置默认值
		self.inputs['距离'].f_setData(0.0)
		self.inputs['方向'].f_setData((0.0, 1.0, 0.0))

		self.use_custom_color = True
		self.color = (0.2,0.5,0.5)
	

class C_NODE_AddMenu_Px平面(bpy.types.Operator):
	bl_idname = 'node.global_node_create_px_plane'
	bl_label = '创建 PhysX 平面'
	bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		bpy.ops.node.add_node(type='C_Node_Px平面', use_transform=True)
		return {'FINISHED'}
'''




def f_update_是否为动态刚体(self, context):
	data = bool(self.m_是否动态)
	engineLib.flexCAPI.f_node_传入BL数据(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), ctypes.py_object(data));
	
class C_Node_Px刚体(基类.C_Node_基类):
	bl_dname = 'bl_node_Px刚体'
	bl_label = '创建 PhysX 刚体'
	#m_物体 = bpy.props.PointerProperty(type=bpy.types.Object, update=f_update_刚体物体)
	#m_转坐标系 = bpy.props.BoolProperty(default=True)
	m_是否动态 = bpy.props.BoolProperty(default=True, update=f_update_是否为动态刚体)
	def init(self, context):
		super(C_Node_Px刚体, self).f_init(context)
		self.inputs.new('C_Socket_物体集', name='物体集')
		self.outputs.new('C_Socket_物理体集', name='物理体集')
		self.f_toCPP()
		
	def f_toCPP(self):
		engineLib.flexCAPI.f_创建物理刚体节点(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), self.f_getPyObject());
		#f_update_刚体物体f_update_刚体物体(self, None)

	def draw_buttons(self, context, layout):
		#layout.prop(self, "m_物体")
		#layout.prop(self, "m_转坐标系")
		layout.prop(self, "m_是否动态")
		
	def f_update(self):
		#f_update_刚体物体(self, None)
		pass

class C_NODE_AddMenu_Px刚体(bpy.types.Operator):
	bl_idname = 'node.global_node_create_px_rigid'
	bl_label = '创建 PhysX 刚体'
	bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		bpy.ops.node.add_node(type='C_Node_Px刚体', use_transform=True)
		return {'FINISHED'}








class C_宏(bpy.types.Macro):
	bl_idname = 'scene.bl_idname_def'
	bl_label = 'scene.mbt'

	@classmethod 
	def poll(self, context):
		print('C_宏 poll')
		return True

	def report(self, type, message):
		print('C_宏 report', type, message)



classes = (
	C_Node_Px平面,
	C_NODE_AddMenu_Px平面,

	C_Node_Px刚体,
	C_NODE_AddMenu_Px刚体,

	C_宏,

)

def register():
	for c in classes:
		bpy.utils.register_class(c)
		
def unregister():
	for c in classes:
		bpy.utils.unregister_class(c)

	



